//
//  WPSigleTransPriceCell.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/9.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPSigleTransPriceCell.h"

@interface WPSigleTransPriceCell ()
@property (nonatomic, strong) UITextField * positionTextField;/**< 票号 */
@property (nonatomic, strong) UILabel * originalPriceLabel;/**< 原价 */
@property (nonatomic, strong) UIView * line;/**< 线 */
@property (nonatomic, strong) UILabel * sellPriceIntroLabel;/**< 转卖价说明 */
- (void)initializeAppearance;/**< 一般界面初始化 */


@end
@implementation WPSigleTransPriceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.positionTextField];
    [self addSubview:self.originalPriceLabel];
    [self addSubview:self.sellPriceIntroLabel];
    [self addSubview:self.sellPriceTextField];
    [self addSubview:self.line];
}
#pragma mark - responds events
- (void)textFieldValueChanged:(UITextField *)sender {
    if ([sender.text containsString:@"."]) {
        NSRange range = [sender.text rangeOfString:@"."];
        if (sender.text.length - range.location > 3) {
            sender.text = [sender.text substringToIndex:range.location + 3];
        }
    }
}
#pragma mark - private methods
- (NSMutableAttributedString *)configOrigilalPriceAttributtedStringWithString:(NSString *)str {
    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥ %@", str]];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(234, 27, 47, 1) range:NSMakeRange(2, attributedStr.length-2)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:COLOR_RGB(141, 141, 141, 1) range:NSMakeRange(0, 1)];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()] range:NSMakeRange(0, str.length)];
    return attributedStr;
}
#pragma mark - setter
- (void)setModel:(WPRightManageDetailModel *)model {
    _model = model;
    _positionTextField.text = model.ticketPosition;
    _originalPriceLabel.attributedText = [self configOrigilalPriceAttributtedStringWithString:[NSString stringWithFormat:@"%@", model.price]];
}
- (void)refreshDataWithArr:(NSArray *)arr {
    _positionTextField.text = arr[0];
    _originalPriceLabel.attributedText = [self configOrigilalPriceAttributtedStringWithString:arr[1]]
    ;
}
#pragma mark - getter
- (UITextField *)positionTextField {
    if (!_positionTextField) {
        _positionTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(25*DHFlexibleHorizontalMutiplier(), 0, 150*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"票号：";
            text.leftView = lab;
            text.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(102, 102, 102, 1);
            text.userInteractionEnabled= NO;
            text;
        });
    }
    return _positionTextField;
}
- (UILabel *)originalPriceLabel {
    if (!_originalPriceLabel) {
        _originalPriceLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.positionTextField.frame), CGRectGetMinY(self.positionTextField.frame), 245*DHFlexibleHorizontalMutiplier()- CGRectGetMaxX(self.positionTextField.frame), CGRectGetHeight(self.positionTextField.bounds))];
            lab.textAlignment = NSTextAlignmentRight;
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab;
        });
    }
    return _originalPriceLabel;
}
- (UILabel *)sellPriceIntroLabel {
    if (!_sellPriceIntroLabel) {
        _sellPriceIntroLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.positionTextField.frame), CGRectGetMaxY(self.positionTextField.frame), 70*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"转卖价格";
            lab;
        });
    }
    return _sellPriceIntroLabel;
}
- (UITextField *)sellPriceTextField {
    if (!_sellPriceTextField) {
        _sellPriceTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.sellPriceIntroLabel.frame), CGRectGetMaxY(self.positionTextField.frame), 150*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(102, 102, 102, 1);
            text.keyboardType = UIKeyboardTypeDecimalPad;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.backgroundColor = BGCOLOR;
            text.placeholder = @"请输入转卖价格";
            [text addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
            text;
        });
    }
    return _sellPriceTextField;
}
- (UIView *)line {
    if (!_line) {
        _line = ({
            UIView * line = [[UIView alloc]initWithFrame:CGRectMake(25*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.sellPriceTextField.frame)+ 10*DHFlexibleHorizontalMutiplier(), 220*DHFlexibleHorizontalMutiplier(), 0.5)];
            line.backgroundColor = COLOR_RGB(232, 232, 232, 1);
            line;
        });
    }
    return _line;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
